from peewee import *

db = MySQLDatabase('nemtocr', host='localhost', port=3800, user='root', password='!@mtg23YWZ^&')


class UniversityPlanScore(Model):
    id = PrimaryKeyField()
    year = IntegerField()
    page = IntegerField()
    uid = IntegerField()
    batch = IntegerField()
    ucode = CharField(max_length=32)
    uname = CharField(max_length=64)
    major = CharField(max_length=64)
    major_type_2 = IntegerField()
    plan_number = IntegerField()
    submit_number = IntegerField()
    enter_number = IntegerField()
    score = TextField()
    province = CharField(max_length=16)
    city = CharField(max_length=16)
    province_code = IntegerField()
    city_code = IntegerField()
    major_type = CharField(max_length=16)
    major_code = CharField(max_length=16)
    is_deleted = IntegerField()

    class Meta:
        database = db  # This model uses the "spider" database.
        table_name = 'university_plan_score'

    # 插入单条数据(返回新增id)
    def toInsert(self, row):
        try:
            return UniversityPlanScore.insert(row).execute()
        except Exception as e:
            return ''

    # 批量插入数据
    def toInsertBatch(self, rows):
        with db.atomic():
            UniversityPlanScore.insert_many(rows).execute()

    # 根据id，获取单条记录
    def getOne(self, id):
        try:
            return UniversityPlanScore.select().where(UniversityPlanScore.id == id).get()
        except Exception as e:
            return ''

    # 根据年份查询列表
    def getList(self, year):
        try:
            return UniversityPlanScore.select().where(UniversityPlanScore.year == year).dicts().execute()
        except Exception as e:
            return ''

    # 根据年份查询列表
    def getListBYYearUcode(self, year):
        try:
            return UniversityPlanScore.select().where((UniversityPlanScore.year == year) &
                                                      (UniversityPlanScore.ucode == 10225)).dicts().execute()
        except Exception as e:
            return ''


if __name__ == '__main__':
    for row in UniversityPlanScore.select():
        print(row.name, type(row))
